package org.dentalcard.ui.login;

import org.dentalcard.entity.DecUser;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.DecUserDaoImpl;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.ui.main.MainFrameBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * @user: Vladimir Chernishov
 * @date: 20.12.11
 * @time: 18:31
 */
public class LoginFrameBuilder {

    private final JFrame mainFrame;
    private final JPanel rootPanel;

    public LoginFrameBuilder() {
        mainFrame = new JFrame(I18nManager.getRes("dental.cards.login"));
        rootPanel = new JPanel(new GridBagLayout());
        init();
    }

    private void init() {
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add the ubiquitous "Hello World" label.
        rootPanel.add(new JLabel(I18nManager.getRes("user.name")), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.LINE_END,
                GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 50));
        final JTextField userName = new JTextField();
        rootPanel.add(userName, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 10), 100, 0));

        rootPanel.add(new JLabel(I18nManager.getRes("password")), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.LINE_END,
                GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 50));
        final JPasswordField password = new JPasswordField();
        rootPanel.add(password, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 10), 100, 0));

        JButton submit = new JButton(I18nManager.getRes("submit"));
        ActionListener submitAction = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user = userName.getText();
                String pass = password.getText();
                DecUserDaoImpl dao = new DecUserDaoImpl();
                DecUser decUser = dao.getUser(user, pass);
                if (decUser == null) {
                    JOptionPane.showMessageDialog(mainFrame.getParent(), "User name and Password \n pair not found",
                            "Login Error", JOptionPane.ERROR_MESSAGE);
                } else {
                    mainFrame.dispose();
                    MainFrameBuilder main = MainFrameBuilder.getInstance();
                    main.setDecUser(decUser);
                    main.show();
                }
            }
        };
        password.addActionListener(submitAction);
        submit.addActionListener(submitAction);
        rootPanel.add(submit, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.LINE_END,
                GridBagConstraints.NONE, new Insets(0, 10, 10, 5), 0, 0));

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainFrame.dispose();
                PersistenceManager.close();
            }
        });
        rootPanel.add(cancel, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 10, 10), 0, 0));

        mainFrame.getContentPane().add(rootPanel);

    }

    public void show(){
        mainFrame.setResizable(false);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }
}
